import { createRouter, createWebHistory } from 'vue-router'
import PkIndexView from '../views/pk/PkIndexView.vue'
import RanklistIndexView from '../views/ranklist/RanklistIndexView.vue'
import RecordIndexView from '../views/record/RecordIndexView.vue'
import RecordContentView from '@/views/record/RecordContentView.vue'
import NotFound from '../views/error/NotFound.vue'
import UserBotIndexView from '../views/user/bot/UserBotIndexView.vue'
import UserAccountLoginView from '@/views/user/account/UserAccountLoginView.vue'
import UserAccountRegisterView from '@/views/user/account/UserAccountRegisterView.vue'
import store from '@/store'

const routes = [
  {
    path: '/',
    name: 'home',
    redirect: '/pk/',
    meta: {
      requestAuth: true, // 需要登录
    }
  },
  {
    path: '/pk/',
    name: 'pk_index',
    component: PkIndexView,
     meta: {
      requestAuth: true, // 需要登录
    }
  },
  {
    path: '/ranklist/',
    name: 'ranklist_index',
    component: RanklistIndexView,
     meta: {
      requestAuth: true, // 需要登录
    }
  },
  {
    path: '/record/',
    name: 'record_index',
    component: RecordIndexView,
   meta: {
      requestAuth: true, // 需要登录
    }
  },
  {
    path: '/record/:recordId/',
    name: 'record_content',
    component: RecordContentView,
   meta: {
      requestAuth: true, // 需要登录
    }
  },
  {
    path: '/user/bot/',
    name: 'user_bot_index',
    component: UserBotIndexView,
     meta: {
      requestAuth: true, // 需要登录
    }
  },
  {
    path: '/user/account/register/',
    name: 'user_account_register',
    component: UserAccountRegisterView,
     meta: {
      requestAuth: false, // 不需要登录
    }
  },
   {
    path: '/user/account/login/',
    name: 'user_account_login',
     component: UserAccountLoginView,
    meta: {
      requestAuth: false, // 不需要登录
    }
  },
   {
    path: '/404/',
    name: '404',
    component: NotFound
  },
  {
    path: '/:catchAll(.*)',
    redirect: '/404/'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  // 路由守卫，检查用户是否登录
  if (to.meta.requestAuth && !store.state.user.is_login) {
    next({name : 'user_account_login'})
  }else {
    next(); // 确保一定要调用 next()
  }
})
export default router
